package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketAd;
import com.cskaoyan.bean.po.MarketAdExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.service.adminservice.ManageAdService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 小黑球
 * @datetime 2022/12/28 22:46
 * @description: TODO:
 */
@Service
public class ManageAdServiceImpl implements ManageAdService {
    //要实现Service中的方法需要使用mapper
    @Autowired
    MarketAdMapper manageMapper;

    @Override
    public CommonData<MarketAd> list(BasePageInfo info, String name, String content) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据info做条件查询
        //Criterion的list用来封装条件
        //使用逆向工程生成的方法
        //在example中拼接条件
        //MarketAdExample example=new MarketAdExample();
        MarketAdExample example = new MarketAdExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        //创建的criteria再去调用方法 拼接条件
        MarketAdExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtil.isEmpty(content)) {
            criteria.andContentEqualTo(content);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAd> list = manageMapper.selectByExample(example);

        //获得pageInfo的最终目标是封装一个CommonData
        PageInfo<MarketAd> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketAd create(MarketAd marketAd) {
        //new
        //插入
        //return
        marketAd.setAddTime(new Date());
        manageMapper.insertSelective(marketAd);
        return marketAd;
    }

    @Override
    public void update(MarketAd marketAd) {
        manageMapper.updateByPrimaryKeySelective(marketAd);
    }

    @Override
    public void delete(MarketAd marketAd) {
        marketAd.setDeleted(true);
        manageMapper.updateByPrimaryKeySelective(marketAd);

    }
}
